
#Replication code for Salience Analysis and Figures (Figure 4 and 5). 'International Organizations in National Parliamentary Debates'. Review of International Organizations. Special Issue: Domestic Politics and International Organizations.

#First install and load relevant libraries

library(dplyr)
library(quanteda)
library(haven)
library(gridExtra)
library(cowplot)
library(openxlsx)
library(pscl)
library(stargazer)
library(gamlss)
library(MASS)
library(ggplot2)
library(gridExtra)


#Load in Data

load("Salience_Analysis_and_Figures.RData")

#Regressions in Manuscript

members.only <- subset(salience_panel, salience_panel$member == 2)

nb1 <- glm.nb(formula = ioshare ~  authority + IPI  +legelec +  year + country , data = salience_panel)
nb2 <- glm.nb(formula = ioshare ~  authority + IPI + +legelec + contract  + ratificationyear + issue  + member + radicalshare +  unemployment  + year + country, data = salience_panel)
nb3 <- glm.nb(formula = ioshare ~  authority + IPI + +legelec + contract + ratificationyear + issue  + member + radicalshare +  unemployment+ year + country, data = members.only)

stargazer(nb1,nb2,nb3)

#Robustness with Crises

nb.cr1 <- glm.nb(formula = ioshare ~  authority + IPI  +legelec +  economiccrisis + securitycrisis +  year + country , data = salience_panel)
nb.cr2 <- glm.nb(formula = ioshare ~  authority + IPI + +legelec + contract  + ratificationyear + issue  + member + radicalshare +  unemployment + economiccrisis + securitycrisis + year + country, data = salience_panel)
nb.cr3 <- glm.nb(formula = ioshare ~  authority + IPI + +legelec + contract + ratificationyear + issue  + member + radicalshare +  unemployment + economiccrisis + securitycrisis + year + country, data = members.only)

stargazer(nb.cr1,nb.cr2,nb.cr3)

#Robustness - Country Level Analyses 

at <- subset(salience_panel, salience_panel$country == "AUT")
de <- subset(salience_panel, salience_panel$country == "DEU")
uk <- subset(salience_panel, salience_panel$country == "GBR")
usa <- subset(salience_panel, salience_panel$country == "USA")
nzl <- subset(salience_panel, salience_panel$country == "NZL")
can <- subset(salience_panel, salience_panel$country == "CAN")

at.rob <- glm.nb(formula = ioshare ~  authority + IPI + +legelec + contract  + ratificationyear + issue  + member + radicalshare +  unemployment  + year , data = at)
de.rob <- glm.nb(formula = ioshare ~  authority + IPI + +legelec + contract  + ratificationyear + issue  + member + radicalshare +  unemployment  + year , data = de)
uk.rob <- glm.nb(formula = ioshare ~  authority + IPI + +legelec + contract  + ratificationyear + issue  + member + radicalshare +  unemployment  + year , data = uk)
usa.rob <- glm.nb(formula = ioshare ~  authority + IPI + +legelec + contract  + ratificationyear + issue  + member + radicalshare +  unemployment  + year , data = usa)
nzl.rob <- glm.nb(formula = ioshare ~  authority + IPI + +legelec + contract  + ratificationyear + issue  + member + radicalshare +  unemployment  + year , data = nzl)
can.rob <- glm.nb(formula = ioshare ~  authority + IPI + +legelec + contract  + ratificationyear + issue  + member + radicalshare +  unemployment  + year , data = can)

stargazer(at.rob, de.rob, uk.rob, usa.rob, nzl.rob, can.rob)

#Robustness without top 1 % 

cutoff <- quantile(salience_panel$ioshare, 0.99) 
without.top.1 <- subset(salience_panel, salience_panel$ioshare < 1.219875)
members.without.top.1 <- subset(without.top.1, without.top.1$member == 2)

rob.without.top.1 <- glm.nb(formula = ioshare ~  authority + IPI  +legelec +  year + country , data = without.top.1)
rob.without.top.2 <- glm.nb(formula = ioshare ~  authority + IPI + +legelec + contract  + ratificationyear + issue  + member + radicalshare +  unemployment  + year + country, data = without.top.1)
rob.without.top.3 <- glm.nb(formula = ioshare ~  authority + IPI + +legelec + contract + ratificationyear + issue  + member + radicalshare +  unemployment+ year + country, data = members.without.top.1)

stargazer(rob.without.top.1, rob.without.top.2, rob.without.top.3)

#Figure 4

g2 <- ggplot(nz.agg.final, aes(fill=io, y=mentions, x= country)) + 
  geom_bar(position="stack", stat="identity") + scale_fill_manual(values = c("blue4", "blue3", "blue", "dodgerblue3", "dodgerblue1", "skyblue1", "cadetblue1", "orangered1")) + theme(legend.position="none", axis.text.x=element_blank()) + labs(title="New Zealand",
                                                                                                                                                                                                                                                  x ="", y = "") + scale_y_continuous(breaks = c(0, 12500,25000), labels = c("0", "12,500", "25,000"))  + theme(axis.text.y = element_text(size = 24, face = "bold"), plot.title = element_text(size = 26, face = "bold"), axis.title.y = element_text(size = 24, face = "bold"))

g5 <- ggplot(de.agg.final, aes(fill=io, y=mentions, x= country)) + 
  geom_bar(position="stack", stat="identity") + scale_fill_manual(values = c("blue4", "blue3", "blue", "dodgerblue3", "dodgerblue1", "skyblue1", "cadetblue", "orangered1")) + theme(legend.position="none", axis.text.x=element_blank()) + labs(title="Germany",
                                                                                                                                                                                                                                                 x ="", y = "") + scale_y_continuous(breaks = c(0, 38000,76000), labels = c("0", "38,000", "76,000"))  + theme(axis.text.y = element_text(size = 24, face = "bold"), plot.title = element_text(size = 26, face = "bold"), axis.title.y = element_text(size = 24, face = "bold"))

g4 <- ggplot(at.agg.final, aes(fill=io, y=mentions, x= country)) + 
  geom_bar(position="stack", stat="identity") + scale_fill_manual(values = c("blue4", "blue3", "blue", "dodgerblue3", "dodgerblue1", "skyblue1", "cadetblue", "orangered1"))+   theme(legend.position="none", axis.text.x=element_blank()) + labs(title="Austria",
                                                                                                                                                                                                                                                  x ="", y = "Statements in Parliament\n") + scale_y_continuous(breaks = c(0, 21000,42000), labels = c("0", "21,000", "42,000"))  + theme(axis.text.y = element_text(size = 24, face = "bold"), plot.title = element_text(size = 26, face = "bold"), axis.title.y = element_text(size = 24, face = "bold"))
g6 <- ggplot(uk.agg.final, aes(fill=io, y=mentions, x= country)) + 
  geom_bar(position="stack", stat="identity") + scale_fill_manual(values = c("blue4", "blue3", "blue", "dodgerblue3", "dodgerblue1", "skyblue1", "cadetblue", "orangered1"))  +   theme(legend.position="none", axis.text.x=element_blank()) + labs(title="United Kingdom",
                                                                                                                                                                                                                                                    x ="", y = "") + scale_y_continuous(breaks = c(0, 140000,280000), labels = c("0", "140,000", "280,000"))  + theme(axis.text.y = element_text(size = 24, face = "bold"), plot.title = element_text(size = 26, face = "bold"), axis.title.y = element_text(size = 24, face = "bold"))
g1 <- ggplot(can.agg.final, aes(fill=io, y=mentions, x= country)) + 
  geom_bar(position="stack", stat="identity") + scale_fill_manual(values = c("blue4", "blue3", "blue", "dodgerblue3", "dodgerblue1", "skyblue1", "cadetblue", "orangered1")) +   theme(legend.position="none", axis.text.x=element_blank()) + labs(title="Canada",
                                                                                                                                                                                                                                                   x ="", y = "Statements in Parliament\n") + scale_y_continuous(breaks = c(0, 50500,101000), labels = c("0", "50,500", "101,000"))  + theme(axis.text.y = element_text(size = 24, face = "bold"), plot.title = element_text(size = 26, face = "bold"), axis.title.y = element_text(size = 24, face = "bold"))

g3 <- ggplot(us.agg.final, aes(fill=io, y=mentions, x= country)) + 
  geom_bar(position="stack", stat="identity") + scale_fill_manual(values = c("blue4", "blue3", "blue", "dodgerblue3", "dodgerblue1", "skyblue1", "cadetblue", "orangered1")) +   theme(legend.position="none", axis.text.x=element_blank()) + labs(title="United States",
                                                                                                                                                                                                                                                   x ="", y = "") + scale_y_continuous(breaks = c(0, 65500,131000), labels = c("0", "65,500", "131,000"))  + theme(axis.text.y = element_text(size = 24, face = "bold"), plot.title = element_text(size = 26, face = "bold"), axis.title.y = element_text(size = 24, face = "bold"))

grid.arrange(g1, g2, g3, g4, g5, g6, ncol =3)

#Figure 5 

g2.fig5 <- ggplot(nz.overtime, aes(x = year, y = iosalience, group = io, col=io, fill=io))+
  geom_line(size = 0.5) + ylab("IO Share of Parliamentary Communication\n")+  scale_colour_manual(values = c("grey", "grey","grey","grey","grey","blue4","grey"))  + annotate("text", x = 2004 , y = 0.027, label = "EU", colour = "white", size = 4, fontface = "bold") + annotate("text", x = 2004 , y = 0.013, label = "UN", colour = "blue4", size = 3, fontface = "bold") + 
  scale_x_continuous(breaks=seq(1990, 2016, 5)) + labs(title="New Zealand",
                                                       x ="", y = "") + theme_classic() + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), axis.title.x = element_blank())  + theme(legend.position="none") + theme(plot.title = element_text(size = 11)) + ylim (0, 0.12)

g4.fig5 <- ggplot(at.overtime, aes(x = year, y = iosalience, group = io, col=io, fill=io))+
  geom_line(size = 0.5) + ylab("IO Share of Parliamentary Communication\n")+  scale_colour_manual(values = c("grey", "blue4","grey","grey","grey","grey","grey", "grey"))  + annotate("text", x = 2016 , y = 0.06, label = "EU", colour = "blue4", size = 3, fontface = "bold") +
  scale_x_continuous(breaks=seq(1996, 2018, 5)) + labs(title="Austria",
                                                       x ="", y = "Share of Parl. Comms.") + theme_classic() + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), axis.title.x = element_blank())  + theme(legend.position="none") +  theme(plot.title = element_text(size = 11), axis.title=element_text(size=10)) + ylim (0, 0.12)

g5.fig5 <- ggplot(de.overtime, aes(x = year, y = iosalience, group = io, col=io, fill=io))+
  geom_line(size = 0.5) + ylab("IO Share of Parliamentary Communication\n")+  scale_colour_manual(values = c("grey", "blue4","grey","grey","grey","grey","grey", "grey"))  + annotate("text", x = 2012 , y = 0.046, label = "EU", colour = "blue4", size = 3, fontface = "bold") +
  scale_x_continuous(breaks=seq(1991, 2018, 5)) + labs(title="Germany",
                                                       x ="", y = "") + theme_classic() + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), axis.title.x = element_blank())  + theme(legend.position="none") + theme(plot.title = element_text(size = 11)) + ylim (0, 0.12)

g3.fig5 <- ggplot(usa.overtime, aes(x = year, y = iosalience, group = io, col=io, fill=io))+
  geom_line(size = 0.5) + ylab("IO Share of Parliamentary Communication\n")+  scale_colour_manual(values = c("grey", "grey","grey","blue4","grey","grey","grey", "grey")) + annotate("text", x = 1998 , y = 0.09, label = "NAFTA", colour = "blue4", size = 3, fontface = "bold") +
  scale_x_continuous(breaks=seq(1990, 2016, 5)) + labs(title="USA",
                                                       x ="", y = "") + theme_classic() + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), axis.title.x = element_blank())  + theme(legend.position="none") + theme(plot.title = element_text(size = 11)) 

g1.fig5 <- ggplot(can.overtime, aes(x = year, y = iosalience, group = io, col=io, fill=io))+
  geom_line(size = 0.5) + ylab("IO Share of Parliamentary Communication\n")+  scale_colour_manual(values = c("grey", "grey","grey","blue4","grey","grey","grey", "grey")) + annotate("text", x = 1998 , y = 0.09, label = "NAFTA", colour = "blue4", size = 3, fontface = "bold") +
  scale_x_continuous(breaks=seq(1990, 2018, 5)) + labs(title="Canada",
                                                       x ="", y = "Share of Parl. Comms.") + theme_classic() + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), axis.title.x = element_blank())  + theme(legend.position="none") + theme(plot.title = element_text(size = 11), axis.title=element_text(size=10)) + ylim (0, 0.12)

g6.fig5 <- ggplot(uk.overtime, aes(x = year, y = iosalience, group = io, col=io, fill=io))+
  geom_line(size = 0.5) + ylab("IO Share of Parliamentary Communication\n")+  scale_colour_manual(values = c("grey", "blue4","grey","grey","grey","grey","grey", "grey")) +  annotate("text", x = 2015 , y = 0.09, label = "EU", colour = "blue4", size = 3, fontface = "bold") +  scale_x_continuous(breaks=seq(1990, 2018, 5)) +labs(title="United Kingdom",  x ="", y = "") + theme_classic() + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), axis.title.x = element_blank())+ theme(legend.position="none") + theme(plot.title = element_text(size = 10)) + ylim (0, 0.12)


grid.arrange(g1.fig5, g2.fig5, g3.fig5, g4.fig5, g5.fig5, g6.fig5, ncol =3)
